package com.sjzl.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.sjzl.entity.Board;
import com.sjzl.entity.Post;
import com.sjzl.entity.Reply;
import com.sjzl.entity.User;
import com.sjzl.service.BoardService;
import com.sjzl.service.PostService;
import com.sjzl.service.ReplyService;

@Component("replyServiceImpl")
public class ReplyServiceImpl extends ServiceImpl<Reply> implements ReplyService{

	@Resource(name="postServiceImpl")
	private PostService postService;
	
	@Resource(name="boardServiceImpl")
	private BoardService boardService;
	
	@Override
	public void create(Reply reply) {
		
	}

	@Transactional
	public void create(Reply reply, Integer post_id) {
		User user = (User) ActionContext.getContext().getSession().get("sessionUser");
		Post post = postService.get(Post.class, post_id);
		int replyCount = post.getReplyCount();
		post.setReplyCount(replyCount+1);
		post.setUserLastReplied(user);
		post.setDateLastReplied(new Date());
		reply.setDateCreated(new Date());
		reply.setPost(post);
		reply.setUser(user);
		int floor = this.getDao().getTotalCount("Reply", "post = " + post_id);
		reply.setFloor(floor+1);
		save(reply);
		postService.update(post);
		//更新版块的回帖数
		Board board = boardService.get(Board.class, reply.getPost().getBoard().getBoard().getId());
		board.setReplyCount(board.getReplyCount()+1);
		boardService.update(board);
	}

	@Transactional
	public void createRReply(Reply reply, Integer reply_id) {
		User user = (User) ActionContext.getContext().getSession().get("sessionUser");
		Reply parentReply = this.getDao().get(Reply.class, reply_id);
		reply.setUser(user);
		reply.setDateCreated(new Date());
		reply.setReply(parentReply);
		this.getDao().save(reply);
	}
	
}
